package com.project.api.common;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.LoggerContextListener;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.spi.ContextAwareBase;
import ch.qos.logback.core.spi.LifeCycle;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 设置logback.xml动态变量值
 * #####没效果，原因未知
 */
public class LoggerGlobalValue extends ContextAwareBase implements LoggerContextListener, LifeCycle {

    public String getDate() {
        Date datetime = new Date(); //获取当前的系统时间。
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd") ; //使用了默认的格式创建了一个日期格式化对象。
        return dateFormat.format(datetime); //可以把日期转换转指定格式的字符串
    }

    private boolean started = false;
    @Override
    public boolean isResetResistant() {
        return false;
    }

    @Override
    public void onStart(LoggerContext loggerContext) {
        if (started) {
            return;
        }
        Context context = getContext();
        context.putProperty("NowDate", getDate());
        started = true;
    }

    @Override
    public void onReset(LoggerContext loggerContext) {

    }

    @Override
    public void onStop(LoggerContext loggerContext) {

    }

    @Override
    public void onLevelChange(Logger logger, Level level) {

    }

    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }

    @Override
    public boolean isStarted() {
        return false;
    }
}
